大数据

您所在的位置:网站首页 hbase shell查询某一列 大数据

大数据

2024-01-21 03:35| 来源: 网络整理| 查看: 265

大数据——四种数据库(MySQL,HBase,MongoDB,Redis)操作实例 问题描述:

                                                                    Student学生表

1. 根据上面给出的表格,用Hbase Shell模式设计student学生表格。

     a) 设计完后,用scan指令浏览表的相关信息,给出截图。

     b) 查询zhangsan 的Computer成绩,给出截图。

     c) 修改lisi的Math成绩,改为95,给出截图。

2. 根据上面已经设计出的student,用Hbase API编程。

     a) 添加数据:English:45 Math:89 Computer:100

    b) 获取scofield的English成绩信息

解决问题: 1. 根据上面给出的表格,用Hbase Shell模式设计student学生表格。

设计表代码:(复制粘贴代码时,要删除注释部分,下同)

$ ssh localhost //检测自己的ssh服务器设置

$ cd /usr/local/hadoop

$ ./sbin/start-dfs.sh  //启动Hadoop

$ jps     注:用以查看hadoop是否启动成功

$ cd /usr/local/hbase

$ bin/start-hbase.sh  //启动hbase

$ jps     注:用以查看hbase是否启动成功

$ bin/hbase shell  //打开hbase的shell操作

hbase(main):006:0> create 'student','score' //创建student,其包含一个列族score

hbase(main):007:0> put 'student','zhangsan','score:English','69' //添加行健,列限定符和写入单元格数据

hbase(main):008:0> put 'student','zhangsan','score:Math','86'

hbase(main):009:0> put 'student','zhangsan','score:Computer','77'

hbase(main):010:0> put 'student','lisi','score:English','55'

hbase(main):011:0> put 'student','lisi','score:Math','100'

hbase(main):012:0> put 'student','lisi','score:Computer','88'

对应的Linux终端运行截图:

a) 设计完后,用scan指令浏览表的相关信息,给出截图。

hbase(main):013:0> scan 'student' //查看student表中信息

对应的Linux终端运行截图:

b) 查询zhangsan 的Computer成绩,给出截图。

hbase(main):014:0> get 'student','zhangsan','score:Computer'//查询zhangsan 的Computer成绩

对应的Linux终端运行截图:

c) 修改lisi的Math成绩,改为95,给出截图。

hbase(main):015:0> put 'student','lisi','score:Math','95' //修改lisi的Math成绩,改为95

hbase(main):016:0> get 'student','lisi','score:Math'

对应的Linux终端运行截图:

2. 根据上面已经设计出的student,用Hbase API编程

(1)与使用MySQL的JAVA客户端相类似,建立Java Project,但请注意,建立project时,所使用的执行环境JRE为J2SE-1.5,如下图方框所示

(2)在工程中导入外部jar包:这里只需要导入hbase安装目录中的lib文件中的所有jar包如果没有jar包,可下载:https://download.csdn.net/download/weixin_43042683/12439546)。如下图所示:

a) 添加数据:English:45 Math:89 Computer:100

(1)新建class,并将如下代码复制到.java文件中,调试运行,给出结果截图。

import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; public class hbase_insert { /** * @param args */ //三个静态成员对象 public static Configuration configuration;//管理HBase的配置信息 public static Connection connection;//管理HBase的连接 public static Admin admin; //管理HBase数据库的表信息 public static void main(String[] args) { // TODO Auto-generated method stub configuration = HBaseConfiguration.create();//使用默认的HBase配置文件创建configuration configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");//连接hbase try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } try {//插入的信息 insertRow("student","scofield","score","English","45"); insertRow("student","scofield","score","Math","89"); insertRow("student","scofield","score","Computer","100"); } catch (IOException e) {//异常处理 // TODO Auto-generated catch block e.printStackTrace(); } close(); } public static void insertRow(String tableName,String rowKey,String colFamily, String col,String val) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); } //关闭连接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } }

 Java运行结果:

 

hbase检验结果:使用scan ‘student’查看数据是否被添加成功

b) 获取scofield的English成绩信息

(1)获取scofield的English成绩信息,可以采用如下代码,调试运行,给出截图

import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; public class hbase_query { /** * @param args */ //三个静态成员对象 public static Configuration configuration;//管理HBase的配置信息 public static Connection connection;//管理HBase的连接 public static Admin admin; //管理HBase数据库的表信息 public static void main(String[] args) { // TODO Auto-generated method stub configuration = HBaseConfiguration.create();//使用默认的HBase配置文件创建configuration configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");//连接hbase try{ connection = ConnectionFactory.createConnection(configuration);//连接hbase admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } try { getData("student","scofield","score","English");//获取scofield的English成绩信息 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } close(); } public static void getData(String tableName,String rowKey,String colFamily, String col)throws IOException{ Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.addColumn(colFamily.getBytes(),col.getBytes()); Result result = table.get(get);//从指定的行的某些单元格中取出相应的值 showCell(result); table.close(); } public static void showCell(Result result){//显示结果信息函数 Cell[] cells = result.rawCells(); for(Cell cell:cells){ System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); System.out.println("Timetamp:"+cell.getTimestamp()+" "); System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); } } //关闭连接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } }

Java 运行结果:

PS:最后,别忘了停止hbase

hbase(main):018:0> hbase(main):025:0> exit

$ bin/stop-hbase.sh

linux终端运行截图:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3